//
//  ActionModel.m
//  Mahjong
//
//  Created by 饶首建 on 17/5/22.
//  Copyright © 2017年 com.ikamobile.com. All rights reserved.
//

#import "ActionModel.h"

@implementation ActionModel

+(void)loadActionsByGameId:(NSString*)gameId success:(LoadActionSuccess)success fail:(LoadActionFail)fail{
    NSString *url = [BASE_URL stringByAppendingPathComponent:NSStringFormat(@"Games/%@/actions",gameId)];
    [HttpUtil sendRequestURL:url method:GET param:nil success:^(id response) {
        NSMutableArray *actionArr = [NSMutableArray new];
        NSArray *arr = response;
        for (NSDictionary *d in arr) {
            [actionArr addObject:[ActionModel parseDic:d]];
        }
        success(actionArr);
    } fail:^(NSString *errorStr) {
        fail(errorStr);
    }];
}

+(void)putActionByGameId:(NSString *)gameId actionId:(NSString *)actionId param:(NSDictionary*)param success:(LoadActionSuccess)success fail:(LoadActionFail)fail{
    NSString *url = [BASE_URL stringByAppendingPathComponent:NSStringFormat(@"Games/%@/actions/%@",gameId,actionId)];
    [HttpUtil sendRequestURL:url method:PUT param:param success:^(id response) {
        ActionModel *am = [ActionModel parseDic:response];
        success(am);
    } fail:^(NSString *errorStr) {
        fail(errorStr);
    }];
}

+(void)deleteActionByGameId:(NSString *)gameId actionId:(NSString *)actionId success:(LoadActionSuccess)success fail:(LoadActionFail)fail{
    NSString *url = [BASE_URL stringByAppendingPathComponent:NSStringFormat(@"Games/%@/actions/%@",gameId,actionId)];
    [HttpUtil sendRequestURL:url method:DELETE param:nil success:^(id response) {
        success(response);
    } fail:^(NSString *errorStr) {
        fail(errorStr);
    }];
}

+(ActionModel*)parseDic:(NSDictionary *)d{
    d = [NSDictionary recursionResultJson4NullclassToNullstring:d];
    ActionModel *am = [ActionModel new];
    am.actionId = d[@"id"];
    am.actionType = d[@"type"];
    am.displayName = d[@"displayName"];
    PlayerModel *pm = [PlayerModel new];
    pm.playerId = d[@"owner"][@"id"];
    pm.playerName = d[@"owner"][@"name"];
    am.owner = pm;
    am.attributesArr = d[@"attributes"];
    am.score = [d[@"score"] integerValue];
    am.times = [d[@"times"] integerValue];
    NSMutableArray *targets = [NSMutableArray new];
    NSMutableArray *targetPlayerIds = [NSMutableArray new];
    NSMutableArray *targetPlayerNames = [NSMutableArray new];
    for (NSDictionary *dd in d[@"targets"]) {
        PlayerModel *pm = [PlayerModel parse:dd];
        [targets addObject:pm];
        [targetPlayerIds addObject:pm.playerId];
        [targetPlayerNames addObject:pm.playerName];
    }
    am.targetsArr = targets;
    am.targetPlayerIdArr = targetPlayerIds;
    am.targetPlayerNameArr = targetPlayerNames;
    if ([d[@"transferedTo"] isKindOfClass:[NSDictionary class]]) {
        am.transferedTo = [PlayerModel parse:d[@"transferedTo"]];
    }
    NSMutableArray *caguaArr = [NSMutableArray new];
    NSMutableArray *caguaPlayerIdArr = [NSMutableArray new];
    NSMutableArray *caguaPlayerNameArr = [NSMutableArray new];
    if ([d[@"cagua"] isKindOfClass:[NSArray class]]) {
        for (NSDictionary *dd in d[@"cagua"]) {
            PlayerModel *pm = [PlayerModel parse:dd];
            [caguaArr addObject:pm];
            [caguaPlayerNameArr addObject:pm.playerName];
            [caguaPlayerIdArr addObject:pm.playerId];
        }
    }
    
    am.caguaArr = caguaArr;
    am.caguaPlayerNameArr = caguaPlayerNameArr;
    am.caguaPlayerIdArr = caguaPlayerIdArr;
    
    am.returned = [d[@"returned"]boolValue];
    return am;
}

@end
